<template>
  <el-container class="app-container">
    <!-- 侧边栏 -->
    <el-aside :width="isCollapse ? '64px' : '200px'" class="sidebar-container">
      <div class="logo-container">
        <img src="/vite.svg" alt="Logo" class="logo" v-if="!isCollapse" />
        <el-icon class="logo-icon" v-else><MenuIcon /></el-icon>
      </div>
      <el-menu
        default-active="dashboard"
        class="sidebar-menu"
        :collapse="isCollapse"
        background-color="#0f172a"
        text-color="#94a3b8"
        active-text-color="#ffffff"
        :unique-opened="true"
      >
        <el-menu-item index="dashboard">
          <el-icon><HomeIcon /></el-icon>
          <span v-if="!isCollapse" class="menu-text">仪表盘</span>
        </el-menu-item>

        <el-sub-menu index="supplier">
          <template #title>
            <el-icon><UserFilled /></el-icon>
            <span v-if="!isCollapse" class="menu-text">供应商管理</span>
          </template>
          <el-menu-item index="supplier-list">供应商列表</el-menu-item>
          <el-menu-item index="supplier-audit">资质审核</el-menu-item>
          <el-menu-item index="supplier-risk">风险评级</el-menu-item>
        </el-sub-menu>

        <el-sub-menu index="order">
          <template #title>
            <el-icon><ShoppingCartIcon /></el-icon>
            <span v-if="!isCollapse" class="menu-text">订单管理</span>
          </template>
          <el-menu-item index="order-list">订单列表</el-menu-item>
          <el-menu-item index="order-sync">订单同步</el-menu-item>
          <el-menu-item index="order-abnormal">异常处理</el-menu-item>
        </el-sub-menu>

        <el-sub-menu index="finance">
          <template #title>
            <el-icon><WalletIcon /></el-icon>
            <span v-if="!isCollapse" class="menu-text">财务管理</span>
          </template>
          <el-menu-item index="invoice">发票管理</el-menu-item>
          <el-menu-item index="reconciliation">对账管理</el-menu-item>
          <el-menu-item index="financial-report">财务报表</el-menu-item>
        </el-sub-menu>

        <el-sub-menu index="analysis">
          <template #title>
            <el-icon><PieChartIcon /></el-icon>
            <span v-if="!isCollapse" class="menu-text">数据分析</span>
          </template>
          <el-menu-item index="supplier-analysis">供应商分析</el-menu-item>
          <el-menu-item index="order-analysis">订单分析</el-menu-item>
          <el-menu-item index="performance-analysis">履约分析</el-menu-item>
        </el-sub-menu>

        <el-menu-item index="system">
          <el-icon><SettingIcon /></el-icon>
          <span v-if="!isCollapse" class="menu-text">系统设置</span>
        </el-menu-item>
      </el-menu>
    </el-aside>

    <!-- 主内容区 -->
    <el-container class="main-container">
      <!-- 顶部导航 -->
      <el-header class="header-container">
        <div class="header-left">
          <el-button
            icon="Menu"
            size="small"
            class="menu-toggle-btn"
            @click="toggleSidebar"
          ></el-button>
        </div>
        <div class="header-right">
          <el-dropdown placement="bottom-end">
            <el-button type="text" class="user-info-btn">
              <el-avatar class="user-avatar" :size="32">
                <img src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png" alt="User Avatar" />
              </el-avatar>
              <span class="user-name" v-if="!isMobile">管理员</span>
              <el-icon class="user-arrow" v-if="!isMobile"><ArrowDown /></el-icon>
            </el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item><el-icon><UserIcon /></el-icon> 个人中心</el-dropdown-item>
                <el-dropdown-item><el-icon><SettingIcon /></el-icon> 账户设置</el-dropdown-item>
                <el-dropdown-item divided><el-icon><LogoutIcon /></el-icon> 退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <!-- 页面内容 -->
      <el-main class="content-container">
        <router-view />
      </el-main>

      <!-- 页脚 -->
      <el-footer class="footer-container">
        <div class="footer-content">
          © 2023 政采云CRM系统 v1.2 | 技术支持: CodeMaster-9000
        </div>
      </el-footer>
    </el-container>
  </el-container>
</template>

<script setup lang="ts">
import { ref, onMounted, reactive } from 'vue';
import { Menu as MenuIcon, House as HomeIcon, UserFilled, ArrowDown, Logout } from '@element-plus/icons-vue';

// 状态管理
const isCollapse = ref(false);
const isMobile = ref(false);

// 切换侧边栏折叠状态
const toggleSidebar = () => {
  isCollapse.value = !isCollapse.value;
};

// 响应式布局处理
const handleResize = () => {
  isMobile.value = window.innerWidth < 768;
  if (isMobile.value) {
    isCollapse.value = true;
  }
};

// 初始化
onMounted(() => {
  handleResize();
  window.addEventListener('resize', handleResize);
});
</script>

<style scoped lang="scss">
.app-container {
  height: 100vh;
  overflow: hidden;
}

.sidebar-container {
  background-color: #0f172a;
  color: #ffffff;
  transition: width 0.3s ease;
  height: 100vh;
  position: fixed;
  z-index: 10;
}

.logo-container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 60px;
  border-bottom: 1px solid #1e293b;
  margin-bottom: 10px;
}

.logo {
  height: 32px;
}

.logo-icon {
  color: #ffffff;
  font-size: 24px;
}

.sidebar-menu {
  border-right: none;
  height: calc(100vh - 70px);
}

.menu-text {
  margin-left: 8px;
}

.main-container {
  margin-left: 64px;
  transition: margin-left 0.3s ease;
  height: 100vh;
}

.header-container {
  background-color: #ffffff;
  border-bottom: 1px solid #e2e8f0;
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 60px;
}

.menu-toggle-btn {
  color: #64748b;
  margin-right: 10px;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info-btn {
  display: flex;
  align-items: center;
  color: #334155;
}

.user-avatar {
  margin-right: 8px;
}

.user-name {
  margin-right: 4px;
}

.content-container {
  padding: 20px;
  overflow-y: auto;
  background-color: #f8fafc;
  height: calc(100vh - 120px);
}

.footer-container {
  background-color: #ffffff;
  border-top: 1px solid #e2e8f0;
  text-align: center;
  padding: 10px 0;
  height: 60px;
}

.footer-content {
  color: #94a3b8;
  font-size: 14px;
}

// 当侧边栏展开时调整主内容区边距
:deep(.el-aside:not(.is-collapse)) ~ .main-container {
  margin-left: 200px;
}

// 移动端适配
@media (max-width: 768px) {
  .main-container {
    margin-left: 0;
  }

  .content-container {
    padding: 10px;
  }
}
</style>